import { Injectable, signal } from '@angular/core';

export interface CustomType {
  id: string;
  name: string;
  fields: string[];
}

@Injectable({
  providedIn: 'root'
})
export class TypesService {
  private _types = signal<CustomType[]>([]);

  readonly types = this._types.asReadonly();

  addType(type: CustomType) {
    this._types.update(types => [...types, type]);
  }

  updateType(id: string, updates: Partial<CustomType>) {
    this._types.update(types =>
      types.map(type =>
        type.id === id ? { ...type, ...updates } : type
      )
    );
  }

  removeType(id: string) {
    this._types.update(types => types.filter(type => type.id !== id));
  }

  clearTypes() {
    this._types.set([]);
  }
}
